void get_intfmac(char *name, mac_address *mac) {
	LPADAPTER	lpAdapter = 0;
	DWORD		dwErrorCode;
	PPACKET_OID_DATA  OidData;
	BOOLEAN		Status;

	lpAdapter = PacketOpenAdapter(name + 8);
	if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
	{
		dwErrorCode=GetLastError();
		printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); 

		exit(-1);
	}
	OidData = (PPACKET_OID_DATA)malloc(6 + sizeof(PACKET_OID_DATA));
	if (OidData == NULL) 
	{
		printf("error allocating memory!\n");
		PacketCloseAdapter(lpAdapter);
		exit(-1);
	}
	OidData->Oid = OID_802_3_CURRENT_ADDRESS;

	OidData->Length = 6;
	ZeroMemory(OidData->Data, 6);
	
	Status = PacketRequest(lpAdapter, FALSE, OidData);
	if(Status)
	{
		mac->byte1 = (OidData->Data)[0];
		mac->byte2 = (OidData->Data)[1];
		mac->byte3 = (OidData->Data)[2];
		mac->byte4 = (OidData->Data)[3];
		mac->byte5 = (OidData->Data)[4];
		mac->byte6 = (OidData->Data)[5];
	}
	else
	{
		printf("error retrieving the MAC address of the adapter!\n");
		exit(-1);
	}

	free(OidData);

}

void set_interface(pcap_t **wan, pcap_t **lan, ip_address *wlip, ip_address *wlmask, ip_address *llip, mac_address *wlmac, mac_address *llmac) {
	pcap_if_t *alldevs, *d;
	int inum, i, facecnt = 0; 
	char errbuf[PCAP_ERRBUF_SIZE];
	//retrieve hardware list

	if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }

	/* Print the list */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. ", ++facecnt);
        if (d->description)
			printf(" %s\n", d->description);
        else
            printf(" (No description available)\n");
    }
	
	printf("\nEnter the WAN interface number (1-%d):",facecnt);
    scanf("%d", &inum);
    
    if(inum < 1 || inum > facecnt)
    {
        printf("\nInterface number out of range.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        exit(-1);
    }
    
    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
    /* Open the device */
    if ( (*wan= pcap_open(d->name,          // name of the device
                              65536,            // portion of the packet to capture
                                                // 65536 guarantees that the whole packet will be captured on all the link layers
                              PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
                              1,             // read timeout
                              NULL,             // authentication on the remote machine
                              errbuf            // error buffer
                              ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
        /* Free the device list */
        pcap_freealldevs(alldevs);
        exit(-1);
    }
	
	/* retreive wan interface MAC */
	get_intfmac(d->name, wlmac);
	
	/* retreive wan interface IP */
	wlip->byte1 = d->addresses->addr->sa_data[2];
	wlip->byte2 = d->addresses->addr->sa_data[3];
	wlip->byte3 = d->addresses->addr->sa_data[4];
	wlip->byte4 = d->addresses->addr->sa_data[5];

	/* retreive wan interface mask */
	wlmask->byte1 = d->addresses->netmask->sa_data[2];
	wlmask->byte2 = d->addresses->netmask->sa_data[3];
	wlmask->byte3 = d->addresses->netmask->sa_data[4];
	wlmask->byte4 = d->addresses->netmask->sa_data[5];

	printf("Enter the LAN interface number (1-%d):",facecnt);
    scanf("%d", &inum);
    
    if(inum < 1 || inum > facecnt)
    {
        printf("\nInterface number out of range.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        exit(-1);
    }
    
    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
    /* Open the device */
    if ( (*lan= pcap_open(d->name,          // name of the device
                              65536,            // portion of the packet to capture
                                                // 65536 guarantees that the whole packet will be captured on all the link layers
                              PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
                              1,             // read timeout
                              NULL,             // authentication on the remote machine
                              errbuf            // error buffer
                              ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
        /* Free the device list */
        pcap_freealldevs(alldevs);
        exit(-1);
    }

	/* retreive lan interface MAC */
	get_intfmac(d->name, llmac);

	/* retreive lan interface IP */
	llip->byte1 = d->addresses->addr->sa_data[2];
	llip->byte2 = d->addresses->addr->sa_data[3];
	llip->byte3 = d->addresses->addr->sa_data[4];
	llip->byte4 = d->addresses->addr->sa_data[5];

	pcap_freealldevs(alldevs);
}
